package com.witframework.web.extremetable.tag;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.extremecomponents.table.bean.Table;
import org.extremecomponents.table.core.TableModel;
import org.extremecomponents.util.ExceptionUtils;

/**
 * @jsp.tag name="columns" display-name="ColumnsTag" body-content="JSP" description="Auto generate the columns."
 * @author Jeff Johnston
 */

public class WsColumnsTag extends TagSupport {
	private static Log logger = LogFactory.getLog(WsColumnsTag.class);

	
	private String autoGenerateColumns;

	/**
	 * @jsp.attribute description="A fully qualified class name to a custom AutoGenerateColumns implementation. Could
	 *                also be a named type in the preferences. Used to generate columns on the fly." required="true"
	 *                rtexprvalue="true"
	 */
	public void setAutoGenerateColumns(String autoGenerateColumns) {
		this.autoGenerateColumns = autoGenerateColumns;
	}

	@Override
	public int doEndTag() throws JspException {
		try {
			TableModel model = TagUtils.getModel(this);
			addExtendedAttributes(model.getTableHandler().getTable());
			if (!TagUtils.isIteratingBody(this)) {
				String autoGenerateColumns = TagUtils.evaluateExpressionAsString("autoGenerateColumns", this.autoGenerateColumns, this, pageContext);
				model.addColumns(autoGenerateColumns);
			} else {
				model.setColumnValues();
			}

			return EVAL_PAGE;
		} catch (Exception e) {
			logger.error(ExceptionUtils.formatStackTrace(e));
			throw new JspException("ColumnsTag.doEndTag() Problem: " + ExceptionUtils.formatStackTrace(e));
		}
	}

	public void release() {
		autoGenerateColumns = null;
		super.release();
	}

	public void addExtendedAttributes(Table table) {		
		
	}

	

	
}
